package com.kura.uednews.fragment;

import com.kura.uedews.R;
import com.kura.uednews.uednewsApplication;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class fragment_news_detail extends Fragment {

	TextView descriptionNewsDetails;
	TextView linkFullArticle;
	TextView titleNewsDetails;
	Button backButton;
	uednewsApplication application;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_news_details, container,
				false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		// GET APPLICATION OBJECT
		application = (uednewsApplication) getActivity().getApplication();
		
		// SET DETAIL FOR TITLE INPUT
		titleNewsDetails = (TextView) getActivity().findViewById(R.id.titleNewsDetails);
		titleNewsDetails.setText(Html.fromHtml(application
				.getTitle()));
		
		// SET DETAIL FOR DESCRIPTION INPUT
		descriptionNewsDetails = (TextView) getActivity().findViewById(
				R.id.descriptionNewsDetails);
		descriptionNewsDetails.setMovementMethod(LinkMovementMethod
				.getInstance());
		descriptionNewsDetails.setText(Html.fromHtml(application
				.getDescription()));

		// SET DETAIL FOR LINK BUTTON
		String link = application.getLink();
		if (application.getChoice() == 1) {
			link = "h"+application.getLink();
			link = link.substring(1);
		}else if (application.getChoice() == 7){
			link = link.substring(1, link.length() - 1);
		}

		linkFullArticle = (TextView) getActivity().findViewById(
				R.id.linkFullArticle);

		linkFullArticle.setText(Html.fromHtml("<a href='" + link
				+ "' > <b>&nbsp; >> Chi tiết</b><a>"));
		linkFullArticle.setLinksClickable(true);
		 linkFullArticle.setMovementMethod(LinkMovementMethod
		 .getInstance());

		backButton = (Button) getActivity().findViewById(R.id.backButton);
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentTransaction tx = getFragmentManager()
						.beginTransaction();
				tx.replace(R.id.main, android.app.Fragment.instantiate(
						getActivity().getApplicationContext(),
						"com.kura.uednews.fragment.fragment_news_list"));
				tx.commit();
			}
		});
	}

}
